package com.walk.zworld.ui.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.base.lib.base.utils.ToastUtil
import com.base.lib.base.view.BaseActivity
import com.walk.zworld.R
import com.walk.zworld.api.http.CommonAppConfig
import com.walk.zworld.api.http.HttpClient
import com.walk.zworld.api.http.ParseHttpCallback
import com.alibaba.fastjson.JSONObject


class JoinTuiGuangActivity : BaseActivity() {

    private lateinit var get_my_number: TextView
    private lateinit var my_tuijian_number: TextView
    private lateinit var user_tuijian_number: TextView
    private lateinit var et_login_id: EditText
    private lateinit var btn_register: Button

    override fun onBindLayout(): Int  = R.layout.activity_join_tuiguang

    override fun initView() {
        get_my_number = findViewById<TextView>(R.id.get_my_number)
        my_tuijian_number = findViewById<TextView>(R.id.my_tuijian_number)
        findViewById<TextView>(R.id.copy_btn).setOnClickListener {
            val text = my_tuijian_number.text
            copy(text)
        }
        user_tuijian_number = findViewById<TextView>(R.id.user_tuijian_number)
        et_login_id = findViewById<EditText>(R.id.et_login_id)
        btn_register = findViewById<Button>(R.id.btn_register)
        btn_register.setOnClickListener {
            val uid = CommonAppConfig.getUid()
            val usernumber = et_login_id.text.toString()
            if (usernumber ==  my_tuijian_number.text) {
                ToastUtil.showToast("不能填写自己")
                return@setOnClickListener
            }
            HttpClient.getInstance().post(LOGINDEVICE, LOGINDEVICE)
                .params("type", "setNumber")
                .params("uid", uid)
                .params("usernumber", usernumber)
                .execute (object : ParseHttpCallback<JSONObject?>(){
                    override fun onSuccess(code: Int, msg: String?, info: JSONObject?) {
                        if (code == 200){
                            initData()
                        }else {
                            msg?.let { it1 -> ToastUtil.showToast(it1) }
                        }
                    }
                })
        }
    }
    private val LOGINDEVICE = CommonAppConfig.HOST + "/web/paytuiguang.php"
    override fun initData() {
        setTop("加入推广计划")
        // http://web.nowk.top/web/paytuiguang.php?type=setNumber&uid=12&usernumber=12RA9
        // http://web.nowk.top/web/paytuiguang.php?type=getNumber&uid=12
        val uid = CommonAppConfig.getUid()
        et_login_id.visibility = View.GONE
        btn_register.visibility = View.GONE
        HttpClient.getInstance().post(LOGINDEVICE, LOGINDEVICE)
            .params("type", "getNumber")
            .params("uid", uid)
            .execute (object : ParseHttpCallback<JSONObject?>(){
                override fun onSuccess(code: Int, msg: String?, info: JSONObject?) {
                    if (code == 200 && info != null){
                        val mynumber = info.getString("mynumber");
                        my_tuijian_number.text = mynumber
                        val usernumber = info.getString("usernumber");
                        if (!TextUtils.isEmpty(usernumber)) {
                            user_tuijian_number.text = usernumber
                        }else {
                            et_login_id.visibility = View.VISIBLE
                            btn_register.visibility = View.VISIBLE
                        }
                    }else {
                        msg?.let { ToastUtil.showToast(it) }
                    }
                }
            })
    }

    private fun copy(content: CharSequence) {
        val cm = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("text", content)
        cm.setPrimaryClip(clipData)
        ToastUtil.showToast("复制成功")
    }
}